package com.jidolstar.astro {
	/**
	 * Degree 단위를 가지는 값을 담는 클래스
	 * @author Yongho,Ji (jidolstar@gmail.com)
	 * @since 2009.03.31
	 * @modified 2009.10.30 
	 */ 
	public class ALDMS {
		private var isSetFractionalDegree:Boolean;
		private var isSetDMS:Boolean;
		private var _fractionalDegree:Number;
		private var _degree:int;
		private var _arcminute:int;
		private var _arcsecond:Number;
		public function ALDMS( $degree:int = 0, $arcminute:int = 0, $arcsecond:Number = 0 ) {
			isSetFractionalDegree = false;
			isSetDMS = true;
			_degree = $degree;
			_arcminute = $arcminute;
			_arcsecond = $arcsecond;
		}
		public function setDMS( $degree:int, $arcminute:int, $arcsecond:Number ):void {
			isSetFractionalDegree = false;
			isSetDMS = true;
			_degree = $degree;
			_arcminute = $arcminute;
			_arcsecond = $arcsecond;			
		}
		public function get degree():int {
			if( !isSetDMS ) fractionalDegree2DMS();
			return _degree;
		}
		public function get arcminute():int {
			if( !isSetDMS ) fractionalDegree2DMS();
			return _arcminute;
		}
		public function get arcsecond():Number {
			if( !isSetDMS ) fractionalDegree2DMS();
			return _arcsecond;
		}
		public function get fractionalDegree():Number {
			if( !isSetFractionalDegree ) {
				isSetFractionalDegree = true;
				_fractionalDegree = _degree + _arcminute/60.0 + _arcsecond/3600.0;
			}
			return _fractionalDegree;
		}
		public function set fractionalDegree( $value:Number ):void {
			if( isNaN( $value ) ) {
				throw new ArgumentError( "value is NaN" );
			} 
			isSetFractionalDegree = true;
			isSetDMS = false;
			_fractionalDegree = $value;
		}
		public function toString( $d:String = "°", $arcmin:String = "’", $arcsec:String = "”", $arcsecToFixed:uint = 2, $useSign:Boolean = false ):String {
			var arcminute:String = _arcminute < 10 ? "0" + _arcminute : _arcminute.toString();
			var arcsecond:String = _arcsecond < 10 ? "0" + _arcsecond.toFixed(20) : _arcsecond.toFixed(20);
			var arcseconds:Array = arcsecond.split(".");
			if( $arcsecToFixed > 0 ) {
				arcsecond = arcseconds[0] + $arcsec + "." + String(arcseconds[1]).substr( 0, $arcsecToFixed );
			} else {
				arcsecond = arcseconds[0] + $arcsec;
			}
			var sign:String;
			if( $useSign ) {
				sign = _degree > 0 ? "+" : "";
			} else {
				sign = "";	
			}
			return sign + _degree + $d + arcminute + $arcmin + arcsecond;			
		} 
		private function fractionalDegree2DMS():void {
			var temp:Number;
			_degree = Math.floor( _fractionalDegree );
			temp = _fractionalDegree - _degree;
			temp *= 60.0;
			_arcminute = Math.floor( temp );
			temp -= _arcminute;
			temp *= 60.0;
			_arcsecond = temp;			
		}
		static public function DMS2FractionalDegree( $degree:int, $arcminute:int, $arcsecond:Number ):Number {
			return $degree + $arcminute/60.0 + $arcsecond/3600.0;
		}	
		static public function fractionalDegree2DMS( $fractionalDegree:Number ):ALDMS {
			var temp:Number;
			var degree:int = Math.floor( $fractionalDegree );
			temp = $fractionalDegree - degree;
			temp *= 60.0;
			var arcminute:int = Math.floor( temp );
			temp -= arcminute;
			temp *= 60.0;
			var arcsecond:Number = temp;
			return new ALDMS( degree, arcminute, arcsecond );				
		}	
	}
}